﻿using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace 默写
{
    /// <summary>
    /// 一册书
    /// </summary>
    public class ABook
    {
        /// <summary>
        /// 构建函数
        /// </summary>
        public ABook(int GradeNumber, ABookLevel level)
        {
            Grade = GradeNumber;
            Level = level;
            ImageIndex = ImageIndex.Book;
            Lessons = new List<ALesson>();
        }

        /// <summary>
        /// 年级
        /// </summary>
        /// <value>1-6</value>
        public int Grade
        {
            get;
            set;
        }

        /// <summary>
        /// 每学期所有的课文
        /// </summary>
        /// <value>&gt;1</value>
        public List<ALesson> Lessons
        {
            get;
            set;
        }

        /// <summary>
        /// 上下学期
        /// </summary>
        /// <value>1:上册；2:下册</value>
        public ABookLevel Level
        {
            get;
            set;
        }

        /// <summary>
        /// 书本图片索引值
        /// </summary>
        /// <value>1</value>
        public ImageIndex ImageIndex
        {
            get;
            set;
        }

        /// <summary>
        /// 转换成树节点
        /// </summary>
        public TreeNodeExt ToNode()
        {
            TreeNodeExt rtnTN = new TreeNodeExt("年级" + Grade.ToString() + "：" + Level.ToString(), (int)ImageIndex, (int)ImageIndex);
            rtnTN.Name = Grade.ToString() + ":" + Level.ToString();
            foreach (ALesson alesson in Lessons)
            {
                rtnTN.Nodes.Add(alesson.ToNode());
            }
            return rtnTN;
        }

        /// <summary>
        /// 转换成字符串
        /// </summary>
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("年级" + Grade.ToString() + "：" + Level.ToString());
            sb.Append("\n");
            foreach (ALesson alesson in Lessons)
            {
                sb.Append(alesson.ToString());
                sb.Append("\n");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成pdf格式内容
        /// </summary>
        public Paragraph ToPDF()
        {
            Paragraph paragraph = new Paragraph();
            paragraph.Leading = 42;
            string fontpath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
            BaseFont caiBF = BaseFont.CreateFont(fontpath + @"\STCAIYUN.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font caifont = new Font(caiBF, 32);
            Chunk textchun = new Chunk("年级" + Grade.ToString() + "：" + Level.ToString(), caifont);
            paragraph.Add(textchun);
            paragraph.Add(Chunk.NEWLINE);
            foreach (ALesson alesson in Lessons)
            {
                paragraph.Leading = 30;
                paragraph.Add(alesson.ToPDF());
            }
            return paragraph;

        }

        /// <summary>
        /// 由节点生成ABook对象
        /// </summary>
        public static ABook FromNode(TreeNodeExt anode)
        {
            String[] strarray = anode.Name.Split(':');
            int grade = Convert.ToInt16(strarray[0]);
            ABookLevel booklevel = (ABookLevel)Enum.Parse(typeof(ABookLevel), strarray[1], true);
            ABook abook = new ABook(grade, booklevel);
            foreach (TreeNodeExt nodeext in anode.Nodes) {
                abook.Lessons.Add(ALesson.FromNode(nodeext));
            }
            return abook;
        }

        /// <summary>
        /// 由字符串生成ABook对象
        /// </summary>
        /// <param name="str">输入字符串：格式为
        /// 0:年级
        /// 1:上下册
        /// 2:识字或课文
        /// 3:第几课
        /// 4:认或写
        /// 5:生字
        /// 6:词组</param>
        /// <returns>ABook对象</returns>
        public static ABook FromString(params string[] strarray) 
        {
            if ((strarray[1] == "上册" || strarray[2] == "下册") && (strarray.Length >= 6))
            {
                int grade = Convert.ToInt16(strarray[0]);
                ABookLevel booklevel = (ABookLevel)Enum.Parse(typeof(ABookLevel), strarray[1], true);
                ABook abook = new ABook(grade, booklevel);
                string[] temparray = null;
                Array.Copy(strarray, temparray, strarray.Length - 2);
                ALesson alesson = ALesson.FromString(temparray);
                if (alesson != null)
                {
                    abook.Lessons.Add(alesson);
                }
                return abook;
            }
            else {
                return null;
            }

        }
    }
}
